import { Injectable } from '@angular/core';
import { HttpHeaders, HttpClient, HttpParams } from '@angular/common/http';
import { Observable, of } from 'rxjs';

import { environment } from '../environments/environment';
import { Forum } from '../models/Forum';
import { NewsCategory } from '../models/NewsCategory';
import {AssetType} from '../models/AssetType';

@Injectable()
export class ForumService {

  private baseUrl = environment.apiUrl + 'api/cmsNews';
  private categoryUrl = environment.apiUrl + 'api/newsCategory';

  constructor(
    private http: HttpClient
  ) {}

  getForums(current: number = 0, pageSize: number = environment.pageSize): Observable<{res: Forum[], success: boolean, total: number}> {
    let params: HttpParams = new HttpParams();
    params = params.append('page', String(current + 1));
    params = params.append('pageSize', String(pageSize));
    return this.http.get<{res: Forum[], success: boolean, total: number}>(`${this.baseUrl}/getlist`, { params: params});
  }
  getForum(id: number): Observable<{res: Forum, success: boolean}> {
    return this.http.get<{res: Forum, success: boolean}>(`${this.baseUrl}/getOne?id=${id}`);
  }
  getForumTypes(): Observable<{res: NewsCategory[], success: boolean}> {
    return this.http.get<{res: NewsCategory[], success: boolean}>(`${this.categoryUrl}/getlisttree`);
  }
  addForum(forum: Forum): Observable<{res: Forum, success: boolean}> {
    return this.http.post<{res: Forum, success: boolean}>(`${this.baseUrl}/insertNews`, forum);
  }

  updateForum(forum: Forum): Observable<{res: Forum, success: boolean}> {
    return this.http.patch<{res: Forum, success: boolean}>(`${this.baseUrl}/updateNews`, forum);
  }

  deleteForum(id: number): Observable<{success: boolean}> {
    return this.http.delete<{success: boolean}>(`${this.baseUrl}/deleteNews?id=${id}`);
  }
  getNewsCategory(): Observable<{res: NewsCategory[], success: boolean}> {
    return this.http.get<{res: NewsCategory[], success: boolean}>(`${this.categoryUrl}/getlist`);
  }

  deleteCategory(id: number): Observable<{success: boolean}> {
    return this.http.delete<{success: boolean}>(`${this.categoryUrl}/deleteNewsCategory?id=${id}`);
  }

  insertCategory(category: NewsCategory): Observable<{res: NewsCategory, success: boolean}> {
    return this.http.post<{res: NewsCategory, success: boolean}>(`${this.categoryUrl}/insertNewsCategory`, category);
  }
  updateCategory(category: NewsCategory): Observable<{res: NewsCategory, success: boolean}> {
    return this.http.patch<{res: NewsCategory, success: boolean}>(`${this.categoryUrl}/updateNews`, category);
  }
}
